#!/bin/sh

. /etc/init.d/tc-functions

checkroot

LANG="$2"
CVERSION=4.7.7
VERSION="$(cat data/eversion)"
TCUSER=`cat /etc/sysconfig/tcuser`
CFG="tmp/core17.cfg"
ISODIR="/mnt/sda2/core17"
TESTDIR="/mnt/sda1/boot/iso"
THISDIR=`pwd`
CDLOCATION=/mnt/sda2/distro/Core-$CVERSION.iso
EZTMPDIR=/mnt/sda2/tmp/ezremaster
TCE17=/mnt/sda2/tce17

[ "$LANG" ] && LANG=_$LANG
if [ ! -f /usr/local/tce.installed/mkisofs-tools ]; then
   if [ ! -e /etc/sysconfig/tcedir/optional/mkisofs-tools.tcz ]; then
      sudo -u ${TCUSER} tce-load -w mkisofs-tools
   fi
      sudo -u ${TCUSER} tce-load -is mkisofs-tools
fi
if [ ! -f /usr/local/tce.installed/advcomp ]; then
   if [ ! -e /etc/sysconfig/tcedir/optional/advcomp.tcz ]; then
      sudo -u ${TCUSER} tce-load -w advcomp
   fi
      sudo -u ${TCUSER} tce-load -is advcomp
fi
if [ ! "$1" = "-np" ] && [ ! "$1" = "-npi" ];then
	[ -f tmp/allextensions.lst ] && rm	   tmp/allextensions.lst    
	cp  data/instlist/standard.instlist    	   tmp/allextensions.lst    
#	cat data/instlist/ondemand.instlist  	>> tmp/allextensions.lst
	cat data/instlist/ndiswrapper.instlist  >> tmp/allextensions.lst
	cat data/instlist/wififirmware.instlist >> tmp/allextensions.lst
	cat data/instlist/wifi.instlist 	>> tmp/allextensions.lst
	cat data/instlist/modem.instlist 	>> tmp/allextensions.lst
	echo Xorg-7.6-3d.tcz	  		>> tmp/allextensions.lst
	echo xf86-video-all.tcz	  		>> tmp/allextensions.lst
	echo "cd_location = $CDLOCATION" 	   	  	  	>  $CFG
	echo "temp_dir = $EZTMPDIR"		  	  	  	>> $CFG
	echo "app_extract_initrd = Xlibs.tcz" 	 		  	>> $CFG
	echo "app_extract_initrd = Xorg-7.6-lib.tcz" 		  	>> $CFG
	echo "app_extract_initrd = exquisite.tcz" 	  		>> $CFG
	echo "extract_tcz_script = chroot" 	  		  	>> $CFG
	sed "s/^/app_outside_initrd_onboot = /" tmp/allextensions.lst	>> $CFG
#	echo -n "ezremaster cfg file created.Press Enter key."
#	read gagme
	rm -rf $EZTMPDIR/*
	./remaster.sh $CFG checkimage
	./remaster.sh $CFG extractimage
	#./remaster.sh $CFG bootcode
	sleep 4
#	echo -n "Next apps.Press Enter key."
#	read gagme
	./remaster.sh $CFG apps
fi
if [ ! "$1" = "-npi" ];then
	echo "Prepare finished ----------------------------------------------------"
	rm tmp/onboot/*
	mkdir -p tmp/onboot

#	echo "xf86-video-all.tcz"		>  tmp/onboot/onboot.lst
	echo "Xorg-7.6-3d.tcz"			> tmp/onboot/onboot.lst
	cat data/instlist/standard.instlist 	>> tmp/onboot/onboot.lst
	
#	echo "xf86-video-all.tcz"		>  tmp/onboot/onbootwifi.lst
	echo "Xorg-7.6-3d.tcz"			>  tmp/onboot/onbootwifi.lst
	cat data/instlist/standard.instlist 	>> tmp/onboot/onbootwifi.lst
	cat data/instlist/ndiswrapper.instlist  >> tmp/onboot/onbootwifi.lst
	cat data/instlist/wififirmware.instlist >> tmp/onboot/onbootwifi.lst
	cat data/instlist/wifi.instlist 	>> tmp/onboot/onbootwifi.lst
	
#	echo "xf86-video-all.tcz"		>  tmp/onboot/onbootmodem.lst
	echo "Xorg-7.6-3d.tcz"			>  tmp/onboot/onbootmodem.lst
	cat data/instlist/standard.instlist 	>> tmp/onboot/onbootmodem.lst
	cat data/instlist/modem.instlist  	>> tmp/onboot/onbootmodem.lst

	
	cp  data/instlist/standard.instlist 	   tmp/onboot/onbootno3d.lst

#	echo "Xorg-7.6.tcz"			>  tmp/onboot/onbootno3dwifi.lst
	cp  data/instlist/standard.instlist 	   tmp/onboot/onbootno3dwifi.lst
	cat data/instlist/ndiswrapper.instlist  >> tmp/onboot/onbootno3dwifi.lst
	cat data/instlist/wififirmware.instlist >> tmp/onboot/onbootno3dwifi.lst
	cat data/instlist/wifi.instlist 	>> tmp/onboot/onbootno3dwifi.lst

#	echo "Xorg-7.6.tcz"			>  tmp/onboot/onbootno3dmodem.lst
	cp  data/instlist/standard.instlist 	   tmp/onboot/onbootno3dmodem.lst
	cat data/instlist/modem.instlist  	>> tmp/onboot/onbootno3dmodem.lst
	#echo -n "onboot files created.Press Enter key."
	#read gagme
	#[ -d $EZTMPDIR/image/cde ] && mv $EZTMPDIR/image/cde $EZTMPDIR/image/tce
	cp data/scripts/tc-config   	$EZTMPDIR/extract/etc/init.d/
	cp data/scripts/e-tc-functions 	$EZTMPDIR/extract/etc/init.d/
	cp data/scripts/tce-setup   	$EZTMPDIR/extract/usr/bin/
	cp data/scripts/fromISOfile 	$EZTMPDIR/extract/usr/bin/
	cp data/scripts/filetool.sh 	$EZTMPDIR/extract/usr/bin/
	cp data/scripts/sethostname	$EZTMPDIR/extract/usr/bin/
	cp data/scripts/bootlocal.sh	$EZTMPDIR/extract/opt/
	cp remaster.sh		 	$EZTMPDIR/extract/usr/local/bin/
	cp data/eversion    		$EZTMPDIR/extract/usr/share/doc/tc/erelease.txt
	mkdir -p		    	$EZTMPDIR/extract/usr/local/share/exquisite/data/themes/
	cp data/core17.edj    		$EZTMPDIR/extract/usr/local/share/exquisite/data/themes/
	mkdir -p			$EZTMPDIR/extract//usr/local/share/pixmaps
	cp logo/corE.png		$EZTMPDIR/extract/usr/local/share/pixmaps/core.png
	cp logo/corE.png		$EZTMPDIR/extract/usr/local/share/pixmaps/logo.png
	[ -d $EZTMPDIR/extract/etc/skel/.e ] && \
			          rm -r $EZTMPDIR/extract/etc/skel/.e
#	cp -a skel/.e   		$EZTMPDIR/extract/etc/skel/
	rm -r 	   			$EZTMPDIR/extract/etc/skel/.e/e/fileman/thumbnails
	[ -d $EZTMPDIR/extract/etc/skel/Desktop ] && \
			          rm -r $EZTMPDIR/extract/etc/skel/Desktop/
	cp -a skel/Desktop/   		$EZTMPDIR/extract/etc/skel/
	cp    skel/.Xdefaults		$EZTMPDIR/extract/etc/skel/
	[ -d $EZTMPDIR/extract/etc/skel/.elementary ] && \
			          rm -r $EZTMPDIR/extract/etc/skel/.elementary
#	cp -a skel/.elementary/		$EZTMPDIR/extract/etc/skel/
	cp    skel/.setbackground	$EZTMPDIR/extract/etc/skel/
	mkdir -p			$EZTMPDIR/extract/usr/local/lib/enlightenment/modules/wizard/extra_desktops/
	mkdir -p			$EZTMPDIR/extract/usr/local/share/enlightenment/data/icons/
	cd 				$EZTMPDIR/extract/usr/local/share/enlightenment/data/icons/
	ln -s video_player.png mplayer.png
	cd "$THISDIR"
	cp data/mplayer-interminal	$EZTMPDIR/extract/usr/local/bin
	chmod +x 			$EZTMPDIR/extract/usr/local/bin/mplayer-interminal
	mkdir -p			$EZTMPDIR/extract/usr/local/share/applications
	cp data/mplayer.desktop		$EZTMPDIR/extract/usr/local/share/applications
	cp data/mplayer-audio.desktop	$EZTMPDIR/extract/usr/local/share/applications
	cp data/mplayer.desktop		$EZTMPDIR/extract/usr/local/lib/enlightenment/modules/wizard/extra_desktops/
	cp data/mplayer-audio.desktop	$EZTMPDIR/extract/usr/local/lib/enlightenment/modules/wizard/extra_desktops/
	mkdir -p 			$EZTMPDIR/extract/usr/local/etc/enlightenment/
	cp data/sysactions.conf		$EZTMPDIR/extract/usr/local/etc/enlightenment/
#	mkdir -p 			$EZTMPDIR/extract/var/lib/connman/
#	cp data/settings		$EZTMPDIR/extract/var/lib/connman/
	cp data/filetool.lst		$EZTMPDIR/extract/opt/.filetool.lst
#	cp -a data/ondemand		$EZTMPDIR/image/cde
#	[ -f $EZTMPDIR/image/boot/core.gz ] && \
#				     rm $EZTMPDIR/image/boot/core.gz
	#cp data/.xsession    		$EZTMPDIR/extract/etc/skel/
	#cp data/entrance.conf    		$EZTMPDIR/extract/usr/local/etc/entrance.conf
	#sleep 4
	#echo -n "Next step package. Press Enter key."
	#read gagme
	./remaster.sh $CFG makeinitrd
	
	[ -f $TCE17/firstrun ] && rm 		$TCE17/firstrun
	rm 			       	  	$TCE17/onboot*
	cp tmp/onboot/*.lst		  	$TCE17/
	cp $EZTMPDIR/image/boot/core17.gz 	$TCE17/boot/
	cp $EZTMPDIR/image/boot/vmlinuz   	$TCE17/boot/
	chgrp -R staff 	 $EZTMPDIR/image
	chmod "g+rw" -R  $EZTMPDIR/image
fi	
#echo -n "Next step makeiso. Press Enter key."
#read gagme
if [ "$LANG" = "_it" ];then 
   cp data/iso/boot_it.msg 	$EZTMPDIR/image/boot/isolinux/boot.msg
else
   cp data/iso/isolinux.cfg 	$EZTMPDIR/image/boot/isolinux
   cp data/iso/boot.msg 	$EZTMPDIR/image/boot/isolinux
   cp data/iso/boot.msg 	$EZTMPDIR/image/boot/isolinux/f1
   cp data/iso/f2		$EZTMPDIR/image/boot/isolinux/f2
fi
cp data/iso/menu.c32		$EZTMPDIR/image/boot/isolinux/
cp data/iso/isolinux.bin	$EZTMPDIR/image/boot/isolinux/
cp tmp/onboot/*.lst		$EZTMPDIR/image/cde
#echo enlightenment.tcz >>	$EZTMPDIR/image/tce/copy2fs.lst
[ -f $EZTMPDIR/image/cde/firstrun ] && \
			rm 	$EZTMPDIR/image/cde/firstrun
cd  $EZTMPDIR/image
#ln -s tce cde
cd "$THISDIR"
./remaster.sh $CFG makeiso
cp $EZTMPDIR/ezremaster.iso "$TESTDIR"
mv $EZTMPDIR/ezremaster.iso "$ISODIR/Core17-$VERSION.iso"

